#include "ItemReference.h"
//#include "ItemReferenceFinder.h"

#include "Defines.h"
#include "CampaignItem.h"



#include <QCoreApplication>



namespace
{
QString REF_FIELD_NAME = "ref";
const char* INVALID_REFERENCE_TEXT = QT_TRANSLATE_NOOP("ItemReference","Invalid Reference");

}


ItemReference::ItemReference(AbstractItem* parent)
    : Item("Reference 1", parent)
//    : AbstractItem(ItemCategory_Reference, parent)
{
    d = new Data();
    setData(CategoryData, ItemCategory_Reference);

    //m_finder = new ItemReferenceFinder(this);

    //connect(m_finder, SIGNAL(sigFoundReference(Item*)), this, SLOT(setReferenceItem(Item*)));

}

ItemReference::ItemReference(Item* refItem, AbstractItem *parent)
    : Item("Reference 2", parent)
//    : AbstractItem(ItemCategory_Reference, parent)
{
    d = new Data();
    setData(CategoryData, ItemCategory_Reference);

    //m_finder = new ItemReferenceFinder(this);

    //connect(m_finder, SIGNAL(sigFoundReference(Item*)), this, SLOT(setReferenceItem(Item*)));

    setReferenceItem(refItem);
}


ItemReference::ItemReference(const QString& refName, AbstractItem *parent)
    : Item("Reference 3", parent)
//    : AbstractItem(ItemCategory_Reference, parent)
{
    d = new Data();
    setData(CategoryData, ItemCategory_Reference);

    //m_finder = new ItemReferenceFinder(this);

    //connect(m_finder, SIGNAL(sigFoundReference(Item*)), this, SLOT(setReferenceItem(Item*)));

    setReferenceName(refName);
}



ItemReference::~ItemReference()
{
    //if (m_finder->isRunning()) m_finder->quit();
}



bool ItemReference::isValid() const
{
    if (d->refItem.isNull()) return false;
    if (d->refName.isEmpty()) return false;
    if (d->refName != d->refItem->objectName()) return false;

    return true;
}



int ItemReference::fieldCount() const
{
    return (ItemReference::DataType_LAST - AbstractItem::DataType_FIRST + 1);
}



QString ItemReference::fieldName(const int &data_type) const
{
    switch (data_type)
    {
    case ValueData:  return REF_FIELD_NAME;
    }

    return QString::null;
}



int ItemReference::field(const QString &field_name) const
{
    if (field_name.toLower() == REF_FIELD_NAME)    return ValueData;

    return AbstractItem::field(field_name);
}


#if 0
void ItemReference::onRefresh()
{
    if (isValid()) return;

    //m_finder->start();
}
#endif


QString ItemReference::referenceName() const
{
    return data(ValueData).toString();
}


Item* ItemReference::referenceItem() const
{
    return d->refItem.data();
}


void ItemReference::setReferenceName(const QString &refName)
{
    setData(ValueData, refName);
}


void ItemReference::setReferenceItem(Item *refItem)
{
    d->refItem = refItem;

    if (isValid()) return;
    if (d->refItem.isNull()) return;

    setReferenceName(d->refItem->objectName());
}


QVariant ItemReference::customData(const int &data_type) const
{
    switch (data_type)
    {
    case Item::ValueData:
        {
            qDebug("Referencd value data");

            if (!isValid()) return qApp->translate("ItemReference", INVALID_REFERENCE_TEXT);

            return d->refName;
        }
    case AbstractItem::DescData:
        {
            qDebug("Referencd desc data");
            return AbstractItem::objectName() + "Hello";
        }

    case AbstractItem::NameData:
        {
            qDebug("Referencd name data");
            return AbstractItem::data(data_type).toString() + "Hello";
        }
    }

    return QVariant();
}


void ItemReference::setCustomData(const int &data_type, const QVariant &value)
{
    switch (data_type)
    {
    case ValueData:
        if (value.toString() == INVALID_REFERENCE_TEXT) return;
        d->refName = value.toString();
        break;

    default:
        Item::setData(data_type, value);
        return;
    }

    if (isValid()) return;

    //m_finder->start();
}
